<template>
  <div class="">
    <div class="wrap">
      <ul class="nav">
        <li
          v-for="(item, index) in cate"
          :key="item._id"
          @click="change(index, item._id)"
          :class="{ active: index == flag }"
        >
          {{ item.title }}
        </li>
      </ul>
    </div>
    <div>
      <van-card
        v-for="item in result"
        :key="item._id"
        :price="item.price"
        :title="item.title"
        :thumb="item.pic"
        @click="gotodetail(item)"
      >
        <template #footer>
          <van-button size="mini" @click.stop="add(item)"
            >加入购物车</van-button
          >
        </template>
      </van-card>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      cate: [],
      list: [],
      flag: 0,
      result: [],
    };
  },
  mounted() {
    this.getcate();
    this.getlist();
  },
  methods: {
    getcate() {
      this.$axios.get("fenlei.json").then((res) => {
        this.cate = res.data.result;
      });
    },
    getlist() {
      this.$axios.get("list.json").then((res) => {
        this.list = res.data.result;
        this.change(0, "59f1e1ada1da8b15d42234e9");
      });
    },
    change(index, id) {
      this.flag = index;
      this.result = this.list.filter((item) => {
        return item.cid == id;
      });
    },
    gotodetail(item) {
      this.$router.push({ path: "/detail", query: { obj: item } });
    },
    add(item) {
      this.$store.commit("add", item);
    },
  },
  components: {},
};
</script>

<style lang="scss" scoped>
.wrap {
  width: 100%;
  overflow: scroll;
  .nav {
    display: flex;
    li {
      width: 80px;
      flex-shrink: 0;
      border: 1px solid #ccc;
      height: 35px;
      line-height: 35px;
    }
  }
  .active {
    background: seagreen;
    color: seashell;
  }
}
</style>
